import 'package:demo1/provider/cart.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';
import '../../model/cart_info.dart';
import 'cart_count.dart';

class CartItem extends StatelessWidget {
  final CartInfoMode item;

  CartItem({Key key, this.item}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
        margin: EdgeInsets.fromLTRB(5, 2, 5, 2),
        padding: EdgeInsets.fromLTRB(5, 10, 5, 10),
        decoration: BoxDecoration(
            color: Colors.white,
            border:
                Border(bottom: BorderSide(width: 1, color: Colors.black12))),
        child: Row(
          children: <Widget>[
            _cartCheckBtn(context, item),
            _cartImage(item),
            _cartGoodsName(item),
            _cartPrice(context, item)
          ],
        ));
  }

  /// 多选按钮
  Widget _cartCheckBtn(BuildContext context, item) {
    return Container(
        child: Checkbox(
            value: item.isCheck,
            activeColor: Colors.pink,
            onChanged: (bool val) {
              item.isCheck = val;
              Provider.of<CartProvider>(context, listen: false)
                  .changeCheckState(item);
            }));
  }

  Widget _cartImage(item) {
    return Container(
      width: ScreenUtil().setWidth(150),
      padding: EdgeInsets.all(3.0),
      decoration: BoxDecoration(
          border: Border(bottom: BorderSide(width: 1, color: Colors.black12))),
      child: Image.network(item.images),
    );
  }

  Widget _cartGoodsName(item) {
    return Container(
      width: ScreenUtil().setWidth(300),
      padding: EdgeInsets.all(10),
      alignment: Alignment.topLeft,
      child: Column(
        children: <Widget>[Text(item.goodsName), CartCount(item)],
      ),
    );
  }

  Widget _cartPrice(context, item) {
    return Container(
        width: ScreenUtil().setWidth(150),
        alignment: Alignment.centerRight,
        child: Column(
          children: <Widget>[
            Text('￥${item.price}'),
            Container(
              child: InkWell(
                  onTap: () {
                    Provider.of<CartProvider>(context, listen: false)
                        .deleteOneGoods(item.goodsId);
                  },
                  child: Icon(Icons.delete_forever,
                      color: Colors.black26, size: 30)),
            )
          ],
        ));
  }
}
